using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

namespace FontKonverter {
    /// <summary>
    /// # Khmer Legacy fonts to Khmer Unicode Conversion
    /// </summary>
    public class UnicodeConverter: IConverter {
        #region IConverter Members
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sin">string input as khmer legacy encoding</param>
        /// <param name="lookupData">list for legacy to unicode conversion</param>
        /// <returns>Khmer unicode string</returns>
        public string Converter(string sin, ArrayList lookupData) {
            //if (data == None or type(data) != ListType or len(data) != 2 or type(data[0]) != DictType or type(data[1]) != ListType):
            //    raise TypeError("Wrong data for conversion.")

            //if (type(sin) == unicode):
            //    raise TypeError("Input must not be Unicode string.")

            StringDictionary unicodeDicts = (StringDictionary)lookupData[0];    //# dictionary with character combinations and replacements
            string[] replaceData = (string[])lookupData[1];                     //# list with character replacement values
            StringBuilder sout = new StringBuilder();
            //sout = u''
            int listLength = replaceData.Length;
            int i = 0;
            int end = sin.Length;

            while (i < end)
            {
                foreach (DictionaryEntry entry in unicodeDicts)
                {
                    string key = entry.Key.ToString();
                    if (key.Equals(sin.Substring(i, key.Length)))
                    {
                        sout.Append(entry.Value.ToString());
                        i += key.Length;
                    }
                }

                int n = sin[i];
                if (n < listLength)
                {
                    sout.Append(replaceData[n]);
                }
                else
                {
                    sout.Append(sin[i]);
                }
                i++;
            }
            return sout.ToString();
        }

        #endregion
    }
}
